/**
 * 
 */
package com.base.myproject.client.ui;

import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.tools.SendServlet;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.fx.FxConfig;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

/**
 * 下载excel验证对话框
 * 验证过后出现下载地址
 * @author zgw
 *2009-11-12 上午06:11:00
 * javazgw@gmail.com
 */
public class DownExcelDialog {

	void init()
	{
		
	}
	public void showDialog()
	{
		
		final Dialog d = new Dialog();
		d.setWidth("100");
		
		final FormPanel formpanel = new FormPanel();
		
	//	formpanel.setHeight(300);
		final TextField<String> bname = new TextField<String>();
		bname.setAllowBlank(false);
		bname.setFieldLabel("操作员名称");
		
		final TextField<String> pass = new TextField<String>();
		pass.setAllowBlank(false);
		pass.setFieldLabel("密码");
		pass.setPassword(true);
		
		final Html html = new Html();
		
		
		
		Button getlink = new Button("确定");
		
		formpanel.setLabelWidth(75);
		formpanel.setFieldWidth(100);
		formpanel.add(bname);
		formpanel.add(pass);
		formpanel.add(html);
		formpanel.add(getlink);
		d.setSize(250, 250);
		d.add(formpanel);
		getlink.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				if(formpanel.isValid())
				{
					 SendServlet ss = new SendServlet();
					 try {
						// MessageBox.alert("", "/"+GWT.getModuleName()+"/BaseServlet"+"Action=download&bname="+bname.getValue()+"&pass="+pass.getValue()+"&type=associator", null);
						 System.out.println("./"+GWT.getModuleName()+"/BaseServlet"+"Action=download&bname="+bname.getValue()+"&pass="+pass.getValue()+"&type=associator");
						ss.sendServlet("./BaseServlet", "Action=download&bname="+bname.getValue()+"&pass="+pass.getValue()+"&type=associator", null, null, new RequestCallback(){

								@Override
								public void onError(Request request, Throwable exception) {
								Debug.printf("error---", 4);
									
								}

								@Override
								public void onResponseReceived(Request request,
										Response response) {
									Document doc = XMLParser.parse((String) response
												.getText());
										NodeList list = doc.getElementsByTagName("root");
										Node node = list.item(0);
										Element elem = (Element) node;

										if (elem.getAttribute("status").equals(
												Env.SUCCESS)) {
											
											html.setHtml("<br><a href=\""+GWT.getModuleName()+"/ExcelServlet?link="+elem.getAttribute("linkurl")+"\">下载会员资料</a>");
									//		formpanel.add(new Html("<br><a href=\"gwtbase/ExcelServlet?link="+elem.getAttribute("linkurl")+"\">下载会员资料</a>"));
										//	formpanel.layout();
											Info.display("","操作成功" );
											//d.setVisible(false);
										} else {
											Info.display("","操作失败!!用户名或者密码不正确，或者没有下载权限" );
											d.el().blink(FxConfig.NONE);  
										}
									
								}
								 
							 });
					} catch (RequestException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
			
		});
		d.show();
	}
}
